% path_gt = 'gt_training_lowres';
% path_im = 'input_training_lowres';
% addpath(path_gt);
% addpath(path_im);
image_path='./input_training_lowres/';
gt_path='./gt_training_lowres/';
bg_path='./Background/';
NewImage_path='./NewImage/';
image_amount=length(dir(image_path))-2;
background_amount=length(dir(bg_path))-2;
newImg_num=1;
for i=1:image_amount
    Number=num2str(i);
    if i<10
        Number=strcat('0',Number);
    end
    image_name=strcat(image_path,'GT',Number,'.png');
    gt_name=strcat(gt_path,'GT',Number,'.png');
    image = double(imread(image_name))/255;
    alpha = double(imread(gt_name))/255;
    alpha_slice = alpha(:,:,1);
    [F, B] = solve_FB(image, alpha_slice);
    [h, w, c] = size(alpha);
    for j=1:background_amount
        NO=num2str(j);
        background_name=strcat(bg_path,NO,'.png');
        newBg = double(imread( background_name))/255;
        newBg = imresize(newBg, [h, w]);
        newIm = alpha .* F + (1 - alpha) .* newBg;
        newIm_str=num2str(newImg_num);
        NewImage_name=strcat(NewImage_path,newIm_str,'.png');
        imwrite(newIm,  NewImage_name);
        newImg_num=newImg_num+1;
    end
end


% figure, imshow(image .* alpha);
% figure, imshow(F);
% figure, imshow(image .* (1 - alpha));
% figure, imshow(B);



% rmpath(path_gt);
% rmpath(path_im);